package com.ruoyi.web.controller.rest.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.WebUser;
import com.ruoyi.system.domain.WebUserCardRecharge;
import com.ruoyi.system.domain.WebUserCardRechargeLog;
import com.ruoyi.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务预约订单Controller
 * 
 * @author wsliupan@126.com
 * @date 2021-06-23
 */
@Api(tags = "账单查询")
@RestController
@RequestMapping("/rest/user/card")
public class _WebUserCardController extends BaseController
{

//    @Autowired
//    private IWebServerOrderService webServerOrderService;
//    @Autowired
//    private IWebServerUserService iWebServerUserService;
    @Autowired
    private IWebUserService iWebUserService;
//    @Autowired
//    private ISysDeptService iSysDeptService;
    @Autowired
    private IWebUserCardRechargeLogService iWebUserCardRechargeLogService;
    @Autowired
    private IWebUserCardRechargeService iWebUserCardRechargeService;
    @Autowired
    private IWebUserCardTimeLogService iWebUserCardTimeLogService;
    @Autowired
    private IWebUserCardTimeService iWebUserCardTimeService;

    @ApiOperation("充值卡充值记录")
    @GetMapping("recharge")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true),
//            @ApiImplicitParam(name = "status", value = "状态 1已预约 2已完成 3已取消" ,required = true),
    })
    public AjaxResult rechargeList(Long userId)
    {
        WebUser userData = iWebUserService.getById(userId);
        if (userData == null) {
            return error(Constants.REQUIRED_FAILD);
        }
        WebUserCardRecharge webUserCardRecharge = new WebUserCardRecharge();
        webUserCardRecharge.setUserId(userId);
        List<Map<String,Object>> maps = iWebUserCardRechargeService.listMaps(new QueryWrapper<WebUserCardRecharge>(webUserCardRecharge)
                .select("create_time","money"));

        Map userDataMap = iWebUserCardRechargeService.selectUserData(userId);
        Map<String,Object> map = new HashMap<>();
        map.put("list",maps);
        map.put("userDataMap",userDataMap);

        return AjaxResult.success(map);
    }

    @ApiOperation(value = "充值卡消费记录",notes = "create_time:时间，number：产品数量，residue：剩余金额，money:金额，product_name:产品名称")
    @GetMapping("recharge/log")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true),
//            @ApiImplicitParam(name = "status", value = "状态 1已预约 2已完成 3已取消" ,required = true),
    })
    public AjaxResult list(Long userId)
    {
        WebUser userData = iWebUserService.getById(userId);
        if (userData == null) {
            return error(Constants.REQUIRED_FAILD);
        }
        WebUserCardRechargeLog webUserCardRechargeLog = new WebUserCardRechargeLog();
        webUserCardRechargeLog.setUserId(userId);
        List<Map<String,Object>> maps = iWebUserCardRechargeLogService.listMaps(new QueryWrapper<>(webUserCardRechargeLog)
        .select("create_time","money","residue","number","product_name")
        .orderByDesc("create_time"));

        Map userDataMap = iWebUserCardRechargeService.selectUserData(userId);
        Map<String,Object> map = new HashMap<>();
        map.put("list",maps);
        map.put("userDataMap",userDataMap);

        return AjaxResult.success(map);
    }


    @ApiOperation(value = "次卡消费记录",notes = "create_time:时间，number：数量，residue：剩余，money:总金额，recycle_type:服务类型")
    @GetMapping("cardTimeList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true),
//            @ApiImplicitParam(name = "status", value = "状态 1已预约 2已完成 3已取消" ,required = true),
    })
    public AjaxResult cardTimeList(Long userId)
    {
        WebUser userData = iWebUserService.getById(userId);
        if (userData == null) {
            return error(Constants.REQUIRED_FAILD);
        }
        WebUserCardRechargeLog webUserCardRechargeLog = new WebUserCardRechargeLog();
        webUserCardRechargeLog.setUserId(userId);
        List<Map<String,Object>> maps = iWebUserCardTimeLogService.selectListByUserId(userId);

        Map userDataMap = iWebUserCardTimeService.selectUserData(userId);
        Map<String,Object> map = new HashMap<>();
        map.put("list",maps);
        map.put("userDataMap",userDataMap);

        return AjaxResult.success(map);
    }
}
